package com.tianqu.tms.www.bid.controller;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.AucManagement;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.bid.actionform.BidCarSerchInputForm;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchInputViewHelper;

public class BidCarSerchAddInputController extends TmsController {
	private Logger logger = Logger.getLogger(BidCarSerchAddInputController.class);
	BidCarSerchInputViewHelper carSerch = new BidCarSerchInputViewHelper();

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			AucCarInfo auc = AucCarInfo.getInstance();
			if (!StringUtil.isNullOrBlank(String.valueOf(uc.getAttribute("modifyFlg")))
					&& "1".equals(uc.getAttribute("modifyFlg"))) {

				// 车辆基本信息
				auc = auc.getaucCar(uc.getParameter("vin"));
				carSerch.setFlg("2");
				carSerch.setId(StringUtil.getint(uc.getParameter("id")));
				carSerch.setOldVin(uc.getParameter("vin"));
				// 根据车辆ID查询竞价信息
				AucManagement aucnamagement = AucManagement.getInstance();
				aucnamagement = aucnamagement.getAucInfoBySerialAll(Integer.valueOf(uc.getParameter("id")));
				carSerch.setStartTime(DateUtil.DateToString13(aucnamagement.getStartTime()));
				carSerch.setAuctionDeadline(String.valueOf(aucnamagement.getAuctionDeadline()));
			} else {
				BidCarSerchInputForm form = (BidCarSerchInputForm) uc.getInputForm();
				// 车辆基本信息
				auc = auc.getaucCar(form.getVin().trim());
			}

			// 竞价设置信息
			view(auc);
			carSerch.setVin(auc.getVin());
			Codes code = Codes.getInstance();

			carSerch.setColorList(code
					.getCodeslistByItemId(Codes.VEHICLE_COLOR));// 车辆颜色

			carSerch.setOwnerTypeList(code
					.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list

		
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			// 车辆品牌
			Vendor vendor = Vendor.getInstance();
			if(!StringUtil.isNullOrBlank(carSerch.getVendorId())){
				if("V".equals(carSerch.getVendorId().substring(0,1))){
					carSerch.setFromType(2);
					carSerch.setVendorList(commercialVehicle.findAllVendorInfo());
				}else if("S".equals(carSerch.getVendorId().substring(0,1))){
					carSerch.setFromType(1);
					carSerch.setVendorList(vendor.findAllVendorInfo());
				}
			}
			
			uc.setAttribute(TmsConstants.VIEWHELP, carSerch);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 赋值转换
	 * 
	 * @param auc
	 * @return
	 */
	private BidCarSerchInputViewHelper view(AucCarInfo auc) {
		if (auc != null && auc.getSerial() != null) {
			carSerch.setSerial(auc.getSerial());
			carSerch.setVin(auc.getVin());
			if ("".equals(auc.getLicensePlate()))
			{
				carSerch.setLicensePlate(LicensePlateConfigConstant.defLicensePlate);
			}
			else {
				carSerch.setLicensePlate(auc.getLicensePlate());
			}
			carSerch.setModelCode(auc.getModelCode());
			carSerch.setVendorCode(auc.getVendorCode());
			carSerch.setRegisterCardNo(auc.getRegisterCardNo());
			carSerch.setEngine(auc.getEngine());
			carSerch.setRegDate(auc.getRegDate());
			carSerch.setYearCarefulPeriod(auc.getYearCarefulPeriod());
			carSerch.setJqx(auc.getJqx());
			carSerch.setSendCardDate(auc.getSendCardDate());
			carSerch.setColor(StringUtil.getString(auc.getColor()));
			carSerch.setDisplacement(auc.getDisplacement());
			carSerch.setGearbox(auc.getGearbox());
			carSerch.setCarUse(StringUtil.getString(auc.getCarUse()));
			carSerch.setCarType(StringUtil.getString(auc.getCarType()));
			carSerch.setCardType(StringUtil.getString(auc.getCardType()));
			carSerch.setCountryStyle(String.valueOf(auc.getCountryStyle()));
			carSerch.setFuelType(StringUtil.getString(auc.getFuelType()));
			carSerch.setLoadquality(StringUtil.getString(auc.getLoadquality()));
			carSerch.setPassenger(StringUtil.getString(auc.getPassenger()));
			carSerch.setOwnerType(StringUtil.getString(auc.getOwnerType()));
			carSerch.setCarOwner(auc.getCarOwner());
			carSerch.setCarOwnerIdCardNo(auc.getCarOwnerIdCardNo());
			carSerch.setOwnerOrgNo(auc.getOwnerOrgNo());
			carSerch.setOwnerPhone(auc.getOwnerPhone());
			carSerch.setOwnerAddress(auc.getOwnerAddress());
			carSerch.setBrandId(auc.getBrandId());
			carSerch.setCarStyleId(auc.getCarStyleId());
			carSerch.setVendorId(auc.getVendorId());
			carSerch.setMileage(String.valueOf(auc.getMileage()));
			carSerch.setPrice(String.valueOf(auc.getPrice()));
			carSerch.setContactPerson(auc.getContactPerson());
			carSerch.setContactPhone(auc.getContactPhone());
			carSerch.setOwnerSex(auc.getOwnerSex());
			carSerch.setOwnerBirthday(auc.getOwnerBirthday());
			carSerch.setImages1(auc.getPhoto1());
			carSerch.setImages2(auc.getPhoto2());
			carSerch.setImages3(auc.getPhoto3());
			carSerch.setImages4(auc.getPhoto4());
			carSerch.setImages5(auc.getPhoto5());
			carSerch.setImages6(auc.getPhoto6());
			carSerch.setImages7(auc.getPhoto7());
			carSerch.setImages8(auc.getPhoto8());
			carSerch.setImages9(auc.getPhoto9());
			carSerch.setImages10(auc.getPhoto10());
			carSerch.setImages11(auc.getPhoto11());
			carSerch.setImages12(auc.getPhoto12());
			carSerch.setImages13(auc.getPhoto13());
			carSerch.setImages14(auc.getPhoto14());
			carSerch.setImages15(auc.getPhoto15());
			carSerch.setImages16(auc.getPhoto16());
			carSerch.setImages17(auc.getPhoto17());
			carSerch.setImages18(auc.getPhoto18());
			carSerch.setImages19(auc.getPhoto19());
			carSerch.setImages20(auc.getPhoto20());
			carSerch.setImages21(auc.getPhoto21());
			carSerch.setImages22(auc.getPhoto22());// 图片从8张增加到20张  modify by mufeiyu
		}
		else {
			carSerch.setLicensePlate(LicensePlateConfigConstant.defLicensePlate);
		}

		return carSerch;

	}
}
